﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace TOP
{
    public enum OptionFunction { name, value, text, number, array, action, end }

    public partial class OptionsCollection
    {
        Dictionary<string, OptionsCollection> values;
        String value;
        OptionsCollection parent;
        static Dictionary<string, string> environmentVars;

        /// <summary>
        /// This is just a boolean value which indicates wheter the option DebugMode was set to true
        /// </summary>
        public static bool DebugMode = false;

        /// <summary>
        /// All variables to replace "[varname]" in the file to load
        /// </summary>
        public static Dictionary<string, string> EnvironmentVars
        {
            get
            {
                if (environmentVars == null) environmentVars = new Dictionary<string, string>();
                return environmentVars;
            }
            set
            {
                environmentVars = value;
            }
        }

        public OptionsCollection(string value)
        {
            values = new Dictionary<string, OptionsCollection>();
            this.value = value;
        }

        public OptionsCollection(string value, OptionsCollection parent)
        {
            values = new Dictionary<string, OptionsCollection>();
            this.value = value;
            this.parent = parent;
        }

        public OptionsCollection Add(string name, string value)
        {
            this.values.Add(name, new OptionsCollection(value, this));
            return this.values[name];
        }

        /// <summary>
        /// Add an item to the list of sub-items
        /// </summary>
        /// <param name="name">name for the item</param>
        /// <param name="OC">item to add</param>
        /// <returns></returns>
        public OptionsCollection Add(string name, OptionsCollection OC)
        {
            this.values.Add(name, OC);
            return this.values[name];
        }

        /// <summary>
        /// Remove a certain item from the sub-items
        /// </summary>
        /// <param name="name">the name of the node</param>
        public void Remove(string name)
        {
            this.values.Remove(name);
        }

        /// <summary>
        /// Get an item contained by this node
        /// </summary>
        /// <param name="name">the name of the node</param>
        /// <returns>the contained node</returns>
        public OptionsCollection Get(string name)
        {
            string[] names = name.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (names.Length != 1)
            {
                if (values.ContainsKey(names[0]))
                    return values[names[0]].Get(name.Substring(name.IndexOf(".") + 1));
                else
                    return new OptionsCollection("not found!");
            }
            else
            {
                if (values.ContainsKey(names[0]))
                    return values[names[0]];
                else
                    return new OptionsCollection("not found!");
            }
        }

        /// <summary>
        /// The value of this OptionsCollection
        /// </summary>
        public string Value
        {
            get { return this.value; }
            private set
            {
                this.value = value;
            }
        }

        /// <summary>
        /// The parent OptionsCollection-node of this instance
        /// </summary>
        public OptionsCollection Parent
        {
            get { return this.parent; }
        }

        /// <summary>
        /// The names of the sub-items of this OptionsCollection
        /// </summary>
        public List<string> SubTree
        {
            get { return values.Keys.ToList<string>(); }
        }

        /// <summary>
        /// Get the value as System.Drawing.Size
        /// </summary>
        /// <returns>The value as System.Drawing.Size</returns>
        public Size ValueAsSize()
        {
            try
            {
                int width = int.Parse(value.Split(',')[0]),
                    height = int.Parse(value.Substring(value.IndexOf(',') + 1));
                return new Size(width, height);
            }
            catch
            {
                return new Size();
            }
        }

        /// <summary>
        /// Get the value as System.Drawing.Point
        /// </summary>
        /// <returns>The value as System.Drawing.Point</returns>
        public Point ValueAsPoint()
        {
            try
            {
                int x = int.Parse(value.Split(',')[0]),
                    y = int.Parse(value.Substring(value.IndexOf(',') + 1));
                return new Point(x, y);
            }
            catch
            {
                return new Point();
            }
        }

        /// <summary>
        /// Get the value as System.Collections.List containing the strings separated by comma's
        /// </summary>
        /// <returns>The value as System.Collections.List</returns>
        public List<string> ValueAsList()
        {
            return value.Split(',').ToList<string>();
        }
    }
}
